package com.sg.zhui.visitor.config;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


/**
 * Created by Anthony on 2016/5/23.
 * Class Note:
 * SharedPreference Helper class
 */
public class PreferencesHelper {
    private static final String SETTING = "setting";
    private static final String TOKEN = "token";
    public static final String USER_INFO = "userInfo";
    public static final String USERSID = "UserSid";
    public static final String LOGINPHONENUM = "loginPhoneNum";




    private final Context context;
    private final SharedPreferences mPref;

    public PreferencesHelper(Context context) {
        this.context = context;
        mPref = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
    }

    public void savePluginApkCode(String apkName, String visitorCode) {
        mPref.edit().putString(apkName, visitorCode).apply();
    }

    public String getPluginApkCode(String apkName) {
        return mPref.getString(apkName, "0");
    }

//    public void saveEntranceCode(String entranceCode) {
//        mPref.edit().putString("entranceCode", entranceCode).apply();
//    }
//
//    public String getEntranceCode() {
//        return mPref.getString("entranceCode", "0");
//    }

    /**
     * 保存文本数据
     *
     * @param spName 文件名
     * @param key    键
     * @param value  值
     */
    public void saveString(String spName, String key, String value) {
        SharedPreferences sp = context.getSharedPreferences(spName, 0);
        boolean commit = sp.edit().putString(key, value).commit();
//		LogOut.e("saveString:", commit+",key:"+key);
    }

    /**
     * 保存int数据
     *
     * @param spName 文件名
     * @param key    键
     * @param value  值
     */
    public void saveInt(String spName, String key, int value) {
        SharedPreferences sp = context.getSharedPreferences(spName, 0);
        sp.edit().putInt(key, value).apply();
    }

    /**
     * 获取保存的Int数据
     *
     * @param spName
     * @param key
     * @param defValue
     * @return
     */
    public int getInt(String spName, String key, int defValue) {
        return context.getSharedPreferences(spName, 0).getInt(key, defValue);
    }

    public void saveBoolean(String spName, String key, boolean value) {
        SharedPreferences sp = context.getSharedPreferences(spName, 0);
        sp.edit().putBoolean(key, value).apply();
    }

    public boolean getBoolean(String spName, String key, boolean defValue) {
        return context.getSharedPreferences(spName, 0).getBoolean(key, defValue);
    }

    /**
     * 删除指定的key
     *
     * @param spName
     * @param key
     */
    public void remove(String spName, String key) {
        context.getSharedPreferences(spName, 0).edit().remove(key).apply();
    }

    public void saveUserSid(String UserSid) {
        mPref.edit().putString(USERSID, UserSid).apply();
    }

    public String getUserSid() {
        return mPref.getString(USERSID, "");
    }

    public void saveLoginPhoneNum(String loginPhoneNum) {
        mPref.edit().putString(LOGINPHONENUM, loginPhoneNum).apply();
    }

    public String getLoginPhoneNum() {
        return mPref.getString(LOGINPHONENUM, "");
    }

    public void saveToken(String token) {
        mPref.edit().putString(TOKEN, token).apply();
    }

    public String getToken() {
        return mPref.getString(TOKEN, "");
    }

    public void saveUserInfo(String userInfo) {
        mPref.edit().putString(USER_INFO, userInfo).apply();
    }

    public String getUserInfo() {
        return mPref.getString(USER_INFO, "");
    }

    public void clear() {
        mPref.edit().clear().apply();
        remove(USER_INFO, USER_INFO);
    }

    /*
     *
     * 保存对象到Share中
     */
    public boolean setObjectToShare(Object object, String key) {
        SharedPreferences share = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        if (object == null) {
            SharedPreferences.Editor editor = share.edit().remove(key);
            return editor.commit();
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        // 将对象放到OutputStream中
        // 将对象转换成byte数组，并将其进行base64编码
        String objectStr = new String(Base64.encode(baos.toByteArray(),
                Base64.DEFAULT));
        try {
            baos.close();
            oos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        SharedPreferences.Editor editor = share.edit();
        // 将编码后的字符串写到base64.xml文件中
        editor.putString(key, objectStr);
        return editor.commit();
    }

    public Object getObjectFromShare(String key) {
        SharedPreferences sharePre = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        try {
            String wordBase64 = sharePre.getString(key, "");
            // 将base64格式字符串还原成byte数组
            if (wordBase64 == null || wordBase64.equals("")) { // 不可少，否则在下面会报java.io.StreamCorruptedException
                return null;
            }
            byte[] objBytes = Base64.decode(wordBase64.getBytes(),
                    Base64.DEFAULT);
            ByteArrayInputStream bais = new ByteArrayInputStream(objBytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            // 将byte数组转换成product对象
            Object obj = ois.readObject();
            bais.close();
            ois.close();
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


//    public void putSignedInRibot(Friends friends) {
//        mPref.edit().putString(PREF_KEY_SIGNED_IN_RIBOT, mGson.toJson(friends)).apply();
//    }

//    @Nullable
//    public Friends getSignedInRibot() {
//        String ribotJson = mPref.getString(PREF_KEY_SIGNED_IN_RIBOT, null);
//        if (ribotJson == null) return null;
//        return mGson.fromJson(ribotJson, Friends.class);
//    }
}
